# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
"""
Dealing Stacks
"""

from ..utillib.misc import kwdefault
from .. import pilelib as stack
from ..gamelib import state
from .. import helpstrings as hs
from . import yukon
from .. import comparecards as cmp_cards
from ..packlib import info as cs


class MultiRKFoundation(stack.Vary_RK_Foundation):

    def __init__(self, x, y, game, suit, **cap):
        """
        __init__(x:int, y:int, game:obj, suit:int[, offset:tup=(0, 0),
          *capability* {max_move=0, base_rank:int=suit, dir:int=suit+1,
          mod=*RANKS*}]) -> None

        :param integer x: left x-coordinate.
        :param integer y: top y-coordinate.
        :param object game: current Game.
        :param integer suit: pile's suit and base_suit
        :keyword tuple offset: offset from left and offset from top

        seealso::
          Module pysollib.stackcaps with details of *capability* keywords.
        """
        ranks = cmp_cards.highestRank(game, suit) + 1
        kwdefault(cap, base_rank=cs.ANY, mod=ranks, dir=suit + 1)
        super(MultiRKFoundation, self).__init__(x, y, game, suit=cs.ANY,
                                                **cap)

    @state.not_state('is_preview')
    def _update_text(self, update_empty=True):
        if not self.texts['misc']:
            return
        RANKS, cap = self.game.RANKS, self.cap
        if not self.cards:
            text = RANKS[cap['base_rank']] if update_empty else ''
        elif len(self.cards) == cap['max_cards']:
            text = ''
        else:
            text = RANKS[(self.TOPCARD['rank'] + cap['dir']) % cap['mod']]
        self.texts['misc'].config(text=text)


class Calculation_Foundation(MultiRKFoundation):
    _BOTTOM_IMAGE_ = 'rank'


class One234_Foundation(MultiRKFoundation):

    @state.not_state('is_preview')
    def _update_text(self, **kw):
        kw.setdefault('update_empty', False)
        super(One234_Foundation, self)._update_text(**kw)


class MultiSSFoundation(stack.SS_Foundation):
    """
    Build up in rank by same suit by suit value step: club 1, spade 2,
    heart 3, diamond 4. Play none. Base rank is suit value.
    """

    def __init__(self, x, y, game, suit, **cap):
        """
        __init__(x:int, y:int, game:obj, suit:int[, offset:tup=(0, 0),
          *capability* {max_move=0, base_rank:int=suit, dir:int=suit+1,
          mod=*RANKS*}]) -> None

        :param integer x: left x-coordinate.
        :param integer y: top y-coordinate.
        :param object game: current Game.
        :param integer suit: pile's suit and base_suit
        :keyword tuple offset: offset from left and offset from top

        seealso::
          Module pysollib.stackcaps with details of *capability* keywords.
        """
        ranks = cmp_cards.highestRank(game, suit) + 1
        kwdefault(cap, base_rank=suit, mod=ranks, dir=suit + 1)
        super(MultiSSFoundation, self).__init__(x, y, game, suit, **cap)


class MultiYukonSSRowStack(yukon.SS_RowStack):
    """
    Build down in rank by same suit by suit value step: club 1, spade 2,
    heart 3, diamond 4.
    """

    def _sequence(self, cards):
        """
        _sequence(cards:seq) -> bool

        Check sequence increases by suit value of base card.

        :return: cards in sequence.
        :rtype: boolean
        """
        return cmp_cards.isSameSuitSequence(cards, dir=-(cards[0]['suit'] + 1),
                                            mod=self.cap['mod'])